﻿using System;
using System.Collections.Generic;

namespace SwyishChess.Domain.Piece
{
    public class Queen : ChessPiece, IVerticallyMovable, IHorizontallyMovable, IDiagonallyMovable
    {
        public const int MAXIMUM_MOVABLE_RANGE = 8;

        public Queen(int owningPlayerId)
        {
            OwningPlayerId = owningPlayerId;
        }

        /* FINDS AND RETURNS A LIST OF COORDINATES WHICH THIS CHESS PIECE CAN POSSIBLY MOVE VERTICALLY. */
        public int[,] GetPossibleVerticalMoves()
        {
            /* SOME OF THE POSSIBLE MOVES, DEENDING ONT HE CURRENT LOCATION OF THE CHESS PIECE,
             * COULD BE MOVING THE CHESS COULD 8 BLOCKS TO THE LEFT (-8) OR 
             * MOVING THE CHESS COULD 8 TO THE RIGHT (+8) */
            int possibleRows = MAXIMUM_MOVABLE_RANGE * -1;
            int[,] possibleVerticalMoves = new int[MAXIMUM_MOVABLE_RANGE * 2, 2];

            for (int row = 0; row < (MAXIMUM_MOVABLE_RANGE * 2); row++)
            {
                /* EXCLUDES THE CHESS PIECES CURRENT LOCATION WHICH WOULD BE [0, 0] */

                possibleVerticalMoves[row, 0] = possibleRows;
                possibleVerticalMoves[row, 1] = 0;

                possibleRows++;
                if (possibleRows == 0)
                    possibleRows++;
            }

            return possibleVerticalMoves;
        }

        /* FINDS AND RETURNS A LIST OF COORDINATES WHICH THIS CHESS PIECES CAN POSSIBLE MOVE HORIZONTALLY. */
        public int[,] GetPossibleHorizontalMoves()
        {
            int possibleColumns = MAXIMUM_MOVABLE_RANGE * -1;
            int[,] possibleHorizontalMoves = new int[MAXIMUM_MOVABLE_RANGE * 2, 2];

            for (int row = 0; row < (MAXIMUM_MOVABLE_RANGE * 2); row++)
            {
                possibleHorizontalMoves[row, 0] = 0;
                possibleHorizontalMoves[row, 1] = possibleColumns;

                possibleColumns++;
                if (possibleColumns == 0)
                    possibleColumns++;
            }

            return possibleHorizontalMoves;
        }

        /* GET ALL POSSIBLE MAIN DIAGONAL MOVES (TOP LEFT TO BOTTOM RIGHT ) */
        public int[,] GetPossibleMainDiagonalMoves()
        {
            int possibleRowAndColumns = MAXIMUM_MOVABLE_RANGE * -1;
            int[,] possibleMainDiagonalMoves = new int[MAXIMUM_MOVABLE_RANGE * 2, 2];

            for (int row = 0; row < (MAXIMUM_MOVABLE_RANGE * 2); row++)
            {
                possibleMainDiagonalMoves[row, 0] = possibleRowAndColumns;
                possibleMainDiagonalMoves[row, 1] = possibleRowAndColumns;

                possibleRowAndColumns++;
                if (possibleRowAndColumns == 0)
                    possibleRowAndColumns++;
            }

            return possibleMainDiagonalMoves;
        }

        /* GET ALL POSSIBLE MAIN DIAGONAL MOVES (BOTTOM LEFT TO TOP RIGHT */
        public int[,] GetPossibleMinorDiagonalMoves()
        {
            int possibleRowAndColumns = MAXIMUM_MOVABLE_RANGE * -1;
            int[,] possibleMinorDiagonalMoves = new int[MAXIMUM_MOVABLE_RANGE * 2, 2];

            for (int row = 0; row < (MAXIMUM_MOVABLE_RANGE * 2); row++)
            {
                if (row < 8)
                {
                    possibleMinorDiagonalMoves[row, 0] = possibleRowAndColumns;
                    possibleMinorDiagonalMoves[row, 1] = possibleRowAndColumns * -1;
                }
                else
                {
                    possibleMinorDiagonalMoves[row, 0] = possibleRowAndColumns * -1;
                    possibleMinorDiagonalMoves[row, 1] = possibleRowAndColumns;
                }

                if (row < 8)
                {
                    possibleRowAndColumns++;
                    if (possibleRowAndColumns == 0)
                        possibleRowAndColumns++;
                }
                else
                {
                    possibleRowAndColumns--;
                    if (possibleRowAndColumns == 0)
                        possibleRowAndColumns--;
                }

            }
            return possibleMinorDiagonalMoves;
        }

        /* FINDS AND RETURNS ALL THE MOVES POSSIBLE BY THIS CHESS PIECE */
        public override List<int[,]> AllPossibleMoves
        {
            get
            {
                List<int[,]> possibleMoves = new List<int[,]>();
                possibleMoves.Add(GetPossibleHorizontalMoves());
                possibleMoves.Add(GetPossibleVerticalMoves());
                possibleMoves.Add(GetPossibleMainDiagonalMoves());
                possibleMoves.Add(GetPossibleMinorDiagonalMoves());
                return possibleMoves;
            }
        }

        public override string ToString()
        {
            return "Queen";
        }
    }
}
